#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "st7735s.h"

#include "lvgl.h"
#include "lv_port_disp.h"
#include "lv_port_indev.h"

#include "gui_guider.h"
#include "events_init.h"
#include "custom.h"
#include "stm32_task.h"
#include "sntp_test.h"
#include "kalman.h"
#include <time.h>

// #include "../lvgl/demos/benchmark/lv_demo_benchmark.h"
lv_ui guider_ui;
#define LVGL_DELAY 10

static void Task_init(void)
{
    lcdInit();
    lcdClear(BLACK);
    LCD_ShowString(42, 68, 48, 16, 16, "System", WHITE, BLACK);
    LCD_ShowString(50, 105, 32, 16, 16, "Init", WHITE, BLACK);
    usleep(1000 *7000);
    lv_init();            // lvgl初始化
    lv_port_disp_init();  // lvgl屏幕驱动初始化
    lv_port_indev_init(); // lvgl输入初始化
    
    stm32_init();
    set_sntp_init(); // sntp初始化
    custom_set_time(&guider_ui);

    setup_ui(&guider_ui);    // 加载UI
    events_init(&guider_ui); // 触发事件初始化
    custom_init(&guider_ui); // 自定义代码初始化
}
static void Smart_TimeKeeper_Tick(void *arg)
{
    (void)arg;
    while(1)
    {
        lv_tick_inc(LVGL_DELAY);    // 计时器
        usleep(LVGL_DELAY*1000);
    }
}
extern void add_log_entry(double speed, float avg_speed, float distance, 
                  const char* time, const char* temp) ;
void Trip_Task(void)
{
    rmc_data rentry;
    int count = osMessageQueueGetCount(qid);
    // printf("[Smart_TimeKeeper_Task Test] osMessageQueueGetCount, count: %d.\r\n",count);
    osMessageQueueGet(qid, (void *)&rentry, NULL, osWaitForever);

    double now_speed = atof(rentry.speed);
    double now_speed_km = now_speed * 1.852;
    float now_speed_m = (float)(now_speed * 0.51);
    char now_speed_data[12]={0};
    sprintf(now_speed_data,"%.2f",now_speed_km);
    lv_label_set_text(guider_ui.Trip_speed, now_speed_data);
    lv_label_set_text(guider_ui.Trip_temp, rentry.Temperature);
    // printf("speed: %d    %d!!\n",now_speed_km, now_speed_m);
    // printf("temp: %s !!\n", rentry.Temperature);

    if(trip_ride_flag == 1) {
        float position_km = (get_position_by_kalman_filter(now_speed_m) / 1000);
        char now_position_km_data[12]={0};
        sprintf(now_position_km_data,"%.1f KM",position_km);
        lv_label_set_text(guider_ui.Trip_km, now_position_km_data);

        static int time = 0;
        time ++;
        char now_time_data[12]={0};
        int totalSeconds = time / 2;
        int hours = totalSeconds / 3600;
        int remainingSeconds = totalSeconds % 3600;
        int minutes = remainingSeconds / 60;
        int seconds = remainingSeconds % 60;
        sprintf(now_time_data,"%02d:%02d:%02d", hours, minutes, seconds);
        lv_label_set_text(guider_ui.Trip_trip_time, now_time_data);

        char now_avg_data[12]={0};
        float avg_speed = (float)((position_km * 3600.0) / totalSeconds);
        sprintf(now_avg_data,"%.1f km/h",avg_speed);
        lv_label_set_text(guider_ui.Trip_avg_speed, now_avg_data);
        //add_log_entry(now_speed_km, avg_speed, position_km, now_time_data, rentry.Temperature);
    }
}
static void Smart_TimeKeeper_Task(void *arg)
{
    (void)arg;
    int Cnt = 0;

    // custom_set_time(ut, &guider_ui);

    //lv_demo_benchmark();
    // lv_demo_benchmark_set_max_speed(true);
    // lv_demo_benchmark_run_scene(69);

    
    while (1)
    {
        lv_task_handler(); // lvgl执行函数
        usleep(LVGL_DELAY*1000);
        Cnt++ ;
        if (Cnt >= 50) {
            Cnt = 0;
            lv_obj_t *current_screen = lv_scr_act();
            if (current_screen == guider_ui.Trip)
            {
                stm32_get();
                Trip_Task();
            }
        }

    }
}

static void Smart_TimeKeeper_Entry(void)
{

    Task_init();
    
    osThreadAttr_t attr_tick;
    attr_tick.name = "Smart_TimeKeeper_Tick";
    attr_tick.attr_bits = 0U;
    attr_tick.cb_mem = NULL;
    attr_tick.cb_size = 0U;
    attr_tick.stack_mem = NULL;
    attr_tick.stack_size = 1024 * 1;
    attr_tick.priority = osPriorityNormal;
    if (osThreadNew(Smart_TimeKeeper_Tick, NULL, &attr_tick) == NULL)
    {
        printf("[Smart_TimeKeeper_Tick] Falied to create Smart_TimeKeeper_Tick!\n");
    }

    osThreadAttr_t attr;
    attr.name = "Smart_TimeKeeper_Task";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024 * 20;
    attr.priority = osPriorityNormal; //24
    if (osThreadNew(Smart_TimeKeeper_Task, NULL, &attr) == NULL)
    {
        printf("[Smart_TimeKeeper_Task] Falied to create Smart_TimeKeeper_Task!\n");
    }
}

SYS_RUN(Smart_TimeKeeper_Entry);
